package com.example.shopping.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝配置类
 * 用于配置支付宝客户端所需的参数
 */
@Configuration
public class AlipayConfig {
    /**
     * 支付宝网关
     * 沙箱环境使用：https://openapi.alipaydev.com/gateway.do
     * 正式环境使用：https://openapi.alipay.com/gateway.do
     */
    private static final String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 应用ID
     * 在支付宝开放平台创建应用后获取
     */
    private static final String APP_ID = "2021000148685333";

    /**
     * 商户私钥
     * 使用支付宝开放平台提供的工具生成
     * 注意：需要妥善保管，不要泄露
     */
    private static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCaiNUkMS19eOaRY0ddkrg2JwqjHuF06iFwwENFNqQeYEwBLnPlUzEKFZg0Tc3XFy1+2dSRbe2ebKadOiRlEfuN6i7UeMQKIQWVS6zDLDn1O45oPK9TAEmii04Jrc311j2Tdh26AMQXiVdVJ5ZY80CCWxvm6wZ0UoEK/thOWyOQ76EjQoRD3y43drhsyAcmzrBjJuIanpPMmZqBvADvmhnFziozi0vGLMFCZEcKcCuIwvOvfYO+Z3LZVdxkFwjBtiitWrWIX6PvYNFqXokbee2CyP+izDD58ar/eFFrv0IHd+/av1IRM/sQZPcSPooIBoRF6AfJTOhsr3ldWHWykpHJAgMBAAECggEBAI6BDiQg4XUqT1zU81Ob4Bm36PxBluDso0fJVH94AuIsJQqV3rQkAj3taf0Znm7x5bdrgrVVbOFCCmdE3ZkCezoEMYknkIhSqUZikX8twoidkhYXmAnvYvfshFsylpJULVZVftYurKWTtiBFsER4lQ1gnRtVsg8EohEUrz2al/KnR465YdW4xCEy2U7qGyMwKvkyQcDIThVg1z1YLvWxcLKD5vvygK1Wcosq5B6nfQAxSOGI1XnLITzt9wDCS868peejvDVVtGmbnfbiBw0fbS8l9r89lSWPGJLmiBboWUaSy+RukzixvUzVZ+Hy1o48HgUN4qyeyfCu+knTaxZAfY0CgYEA2EaMPNEOA4oJlUadj+xO5xvNfaiXFwY9mlkYu5viZgnmrQrXM663MbiSA0T6NQkCTraLYvnAMf9oSxBvqUW2B1VI3SjX2vamHraGU7Pp4LDoTWQghfN/lOMSe/avKWPuTzBX046hIGgKE82TLiJH5HndPHaKyatMxICmEYY27OsCgYEAtusraviFwBjBzsP+79REbmykPe6ULUEsOqgQ9aheAzWN27Kch/SXsLdL9mhiw0Q5MfeNqwOwtTVgj7w3lFhAWDlONDiaMycp6SDRnQ7Ik94+/2Bf60egpKjKJUGv9d/pke2DNiVlPuC/4wm6K4OYPKUx0j0K9ZJKYj2Yd7X9fxsCgYAcrJXRRmJWQ8nrebZQK6N2brA69cw6aEBoo2FtFkjZAH0N+j74AjUVqYPqXQ3TVENzDi7Cy5wYxOUd9qrnRW/R3xPPBqg/xq3Vqpt6o4LhoHLcN+c7+0GyCr0bpGQq163z39P1H5brRf/4Cix2VGM1vpZ/erQB8u01rFzYO/QySwKBgGD8rofIgDqLpe95EqqdVS9roWZJIv4fyVDMnjly7W4sLixoNwELWobTmY5saLl6ag0pYk9j5IUgaTOtK5cXd/6PW/zqajBnOpymzx3Eoettct+7fQBmeTH1YUGB48et0s2SLvk9uMIvLu810CWUX9sACDueLDprN8+BM199+owzAoGAGQ5MDzu8hHuK4Mu6ENlx0AcoFE/IQ2W1uAQhAMJNcgOuWeWlaVmZgZAj+/K8GFXTqDHTKazw0t8N5m5n+ti7IUdLSDRUWHK3YBb32XfysrrxQ5bpPQnwgnTcuX/n1IIAggcqwpcVyrH/gq5Wi8Idz/LkCInQRDFYM/iQkjM1wnk=";

    /**
     * 支付宝公钥
     * 在支付宝开放平台获取
     * 用于验证支付宝的异步通知
     */
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnqcnm4HUxBnsZacDcwck9xoCziuzpd35prn6AW1MjQ6/VS7TjtUZOeV1fCfHCROrUaQA+zTZOqWHwiD/CHsYXOslPyr5MZXE+V/EFL9fbDmbZ2eAk4K7XOkmayUJUn3aTyFJh4OyBh90ppbxtI8D2RB5gEBIgZibf8lH/LhaY996FB+2VJpMwknhBmkd2uDlf8xbE2oGJ1hoWw/544xmSdneA0cgs/3rkVvGkdtkq7YQ+XQ9+6Ass7ZDBREmQYPTNWd49ROmE1vtn0TCwTPU7bbEvUOXakSsqBWESms2uTHbAZaPkvp4+F7e52SA4Os7tndlPNWERqk4W0eUyqu8nQIDAQAB";

    /**
     * 异步通知地址
     * 支付宝服务器主动通知商户服务器里指定的页面http/https路径
     * 建议使用https协议,必须是外网地址，不然支付宝服务器无法返回支付是否成功
     */
    private static final String NOTIFY_URL = "http://localhost:8080/alipay/notify";

    /**
     * 同步返回地址
     * 支付完成后，用户从支付宝页面跳转回商户页面的地址
     */
    private static final String RETURN_URL = "http://localhost:8080/alipay/return";
    /**
     * 创建支付宝客户端
     * @return AlipayClient实例
     */
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
                URL,                // 支付宝网关
                APP_ID,            // 应用ID
                PRIVATE_KEY,       // 商户私钥
                "json",           // 请求格式
                "UTF-8",          // 编码格式
                ALIPAY_PUBLIC_KEY, // 支付宝公钥
                "RSA2"            // 签名方式
        );
    }
}
